package cn.gzmli.dtt.common.controll;

import cn.gzmli.dtt.common.entity.PageInfo;
import cn.gzmli.dtt.common.utils.Constant;
import cn.gzmli.dtt.common.entity.Result;
import cn.gzmli.dtt.common.entity.LoginLog;
import cn.gzmli.dtt.common.service.log.LoginLogService;
import com.xiaoleilu.hutool.util.StrUtil;
import jdk.nashorn.internal.ir.annotations.Ignore;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @Descript 系统登录登出日志
 * @Author xsx
 * @Date 2018/3/21
 */
@Controller
@RequestMapping("/system/log")
@ResponseBody
@Ignore
public class LoginLogController{
    @Autowired
    private LoginLogService loginLogService;

    @RequestMapping("/list")
    public Result list() throws Exception {
        return new Result(Constant.SUCCESS, "获取登录登出日志列表成功", this.loginLogService.getAll());
    }

    @RequestMapping("/listByCondition")
    public Result listByCondition(HttpServletRequest request, PageInfo pageInfo) throws Exception {
        DetachedCriteria condition = DetachedCriteria.forClass(LoginLog.class);
        if (StrUtil.isNotBlank(request.getParameter("type"))){
            condition.add(Restrictions.eq("type", Integer.valueOf(request.getParameter("type"))));
        }
        if (StrUtil.isNotBlank(request.getParameter("loginName"))){
            condition.add(Restrictions.like("loginName", request.getParameter("loginName"), MatchMode.ANYWHERE));
        }
        if (StrUtil.isNotBlank(request.getParameter("userName"))){
            condition.add(Restrictions.like("userName", request.getParameter("userName"), MatchMode.ANYWHERE));
        }
        condition.addOrder(Order.desc("id"));
        List<LoginLog> data = this.loginLogService.getByCondition(condition, pageInfo);
        return new Result(Constant.SUCCESS, "获取登录登出日志列表成功", data, pageInfo);
    }
}
